﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.SqlClient;
namespace DAO
{
    public class LanXBDAO
    {
        #region 1.Them
        public static bool ThemLanXB(LanXBDTO lanxb){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLanXB", lanxb.MaLanXB));
                sqlParams.Add(new SqlParameter("@MaTuaDe", lanxb.MaTuaDe));
                sqlParams.Add(new SqlParameter("@NamXB", lanxb.NamXB));
                sqlParams.Add(new SqlParameter("@KhoGiay", lanxb.KhoGiay));
                sqlParams.Add(new SqlParameter("@SoTrang", lanxb.SoTrang));
                sqlParams.Add(new SqlParameter("@SoLuong", lanxb.SoLuong));
                sqlParams.Add(new SqlParameter("@Gia", lanxb.Gia));
                sqlParams.Add(new SqlParameter("@DiaCD", lanxb.DiaCD));
                sqlParams.Add(new SqlParameter("@MaNXB", lanxb.MaLanXB));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertLANXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Sua
        public static bool SuaLanXB(LanXBDTO lanxb)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLanXB", lanxb.MaLanXB));
                sqlParams.Add(new SqlParameter("@MaTuaDe", lanxb.MaTuaDe));
                sqlParams.Add(new SqlParameter("@NamXB", lanxb.NamXB));
                sqlParams.Add(new SqlParameter("@KhoGiay", lanxb.KhoGiay));
                sqlParams.Add(new SqlParameter("@SoTrang", lanxb.SoTrang));
                sqlParams.Add(new SqlParameter("@SoLuong", lanxb.SoLuong));
                sqlParams.Add(new SqlParameter("@Gia", lanxb.Gia));
                sqlParams.Add(new SqlParameter("@DiaCD", lanxb.DiaCD));
                sqlParams.Add(new SqlParameter("@MaNXB", lanxb.MaLanXB));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateLANXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Xoa
        public static bool XoaLanXB(byte malanxb, String matuade)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLanXB", malanxb));
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteLANXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem
        public static LanXBDTO TimLanXB(byte malanxb, String matuade){
            LanXBDTO result = new LanXBDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLanXB", malanxb));
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLANXB", sqlParams);

                DataRow dr = dt.Rows[0];

                result.MaLanXB = byte.Parse(dr["MaLanXB"].ToString());
                result.MaTuaDe = dr["MaTuaDe"].ToString();
                result.NamXB = int.Parse(dr["NamXB"].ToString());
                result.KhoGiay = dr["KhoGiay"].ToString();
                result.SoTrang = int.Parse(dr["SoTrang"].ToString());
                result.SoLuong = int.Parse(dr["SoLuong"].ToString());
                result.Gia = decimal.Parse(dr["Gia"].ToString());
                result.DiaCD = int.Parse(dr["DiaCD"].ToString());
                result.MaNXB = dr["MaNXB"].ToString();
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaLanXB(){
            //List<LanXBDTO> result = new List<LanXBDTO>();
            DataTable dt= new DataTable();
            try
            {
                 dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLANXBsAll");
                //foreach (DataRow dr in dt.Rows)
                //{
                //    LanXBDTO tmp = new LanXBDTO();

                //    tmp.MaLanXB = SqlDataAccessHelper.ParseByte(dr["MaLanXB"].ToString());
                //    tmp.MaTuaDe = dr["MaTuaDe"].ToString();
                //    tmp.NamXB = SqlDataAccessHelper.ParseInt(dr["NamXB"].ToString());
                //    tmp.KhoGiay = dr["KhoGiay"].ToString();
                //    tmp.SoTrang = SqlDataAccessHelper.ParseInt(dr["SoTrang"].ToString());
                //    tmp.SoLuong = SqlDataAccessHelper.ParseInt(dr["SoLuong"].ToString());
                //    tmp.Gia = SqlDataAccessHelper.ParseDecimal(dr["Gia"].ToString());
                //    tmp.DiaCD = SqlDataAccessHelper.ParseInt(dr["DiaCD"].ToString());
                //    tmp.MaNXB = dr["MaNXB"].ToString();

                //    result.Add(tmp);
                //}
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        public static List<LanXBDTO> TimLanXBByTuaDeSach(String matuade){
            List<LanXBDTO> result = new List<LanXBDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("MaTuaDe", matuade));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLanXBsByAndMaTuaDe", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    LanXBDTO tmp = new LanXBDTO();

                    tmp.MaLanXB = byte.Parse(dr["MaLanXB"].ToString());
                    tmp.MaTuaDe = dr["MaTuaDe"].ToString();
                    tmp.NamXB = int.Parse(dr["NamXB"].ToString());
                    tmp.KhoGiay = dr["KhoGiay"].ToString();
                    tmp.SoTrang = int.Parse(dr["SoTrang"].ToString());
                    tmp.SoLuong = int.Parse(dr["SoLuong"].ToString());
                    tmp.Gia = decimal.Parse(dr["Gia"].ToString());
                    tmp.DiaCD = int.Parse(dr["DiaCD"].ToString());
                    tmp.MaNXB = dr["MaNXB"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool CheckLanXBByID(byte malanxb, string matuade){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLanXB", malanxb));
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLANXB", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
    }
}

